package com.qianli.cashmama.trade.unique.impl;

import com.fqgj.common.utils.DateUtil;
import com.qianli.cashmama.product.facade.result.ProductCategory;
import com.qianli.cashmama.trade.common.enums.UniqueRepeatTypeEnum;
import com.qianli.cashmama.trade.common.enums.UniqueStatusEnum;
import com.qianli.cashmama.trade.dao.TPreventRepeatDao;
import com.qianli.cashmama.trade.entity.TPreventRepeatEntity;
import com.qianli.cashmama.trade.integration.product.ProductIntegrationService;
import com.qianli.cashmama.trade.unique.BillUniqueService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/12/6
 * Time: 下午6:10
 */
@Service
public class BillUniqueServiceImpl implements BillUniqueService {
    private static final Logger LOGGER = LoggerFactory.getLogger(BillUniqueServiceImpl.class);

    @Autowired
    private TPreventRepeatDao preventRepeatDao;

    @Autowired
    private ProductIntegrationService productIntegrationService;

    @Override
    public com.qianli.cashmama.trade.common.enums.UniqueStatusEnum uniqueDecide(String tradeNo, String userCode, String productCategory) {
        ProductCategory productCategoryVo = productIntegrationService.getCategoryByCode(productCategory);
        try {

            TPreventRepeatEntity preventRepeatEntity = new TPreventRepeatEntity()
                    .setRepeatType(UniqueRepeatTypeEnum.CREATE_BILL.getType())
                    .setTradeNo(tradeNo)
                    .setUserCode(userCode)
                    .setMerchantCode(productCategoryVo.getMerchantCode())
                    .setProductCategory(productCategory)
                    .setRepeatVersion("0");

            preventRepeatDao.insert(preventRepeatEntity);

            return com.qianli.cashmama.trade.common.enums.UniqueStatusEnum.NEW;
        } catch (DuplicateKeyException e) {
            LOGGER.error("== 重复创建账单 ==,tradeNo:{},userCode:{},e", tradeNo, userCode, e);
            return UniqueStatusEnum.EXIST;
        }
    }

    @Override
    public boolean releaseUniqueLock(String tradeNo, String userCode, String productCategory) {
        try {
            Map<String, Object> paramMap = new HashMap();
            paramMap.put("userCode", userCode);
            paramMap.put("productCategory", productCategory);
            paramMap.put("repeatType", UniqueRepeatTypeEnum.CREATE_BILL.getType());
            TPreventRepeatEntity preventRepeatEntity = preventRepeatDao.selectOneByParam(paramMap);
            if (preventRepeatEntity != null) {
                preventRepeatEntity.setRepeatVersion(String.valueOf(DateUtil.getMillis()));
                preventRepeatDao.updateRepeatVersion(preventRepeatEntity);
            }

            LOGGER.info("== 释放账单唯一锁成功 ==,userCode:{},tradeNo:{}", userCode, tradeNo);
            return true;
        } catch (Exception e) {
            LOGGER.error("== 释放账单唯一锁失败 ==,userCode:{},tradeNo:{},e", userCode, tradeNo, e);
            return false;
        }
    }
}
